<?php

namespace App\Models;

use App\Util\Tool;
use App\Util\Url;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class SupplierBak extends Model
{
    protected $table = "c_suppliers_bak";

    public function setLicenseImgAttribute($pictures)
    {
        if (is_array($pictures)) {
            $this->attributes['license_img'] = json_encode($pictures);
        }
    }

    public function getLicenseImgAttribute($pictures)
    {
        return json_decode($pictures, true);
    }

    public function getSuppliers($data)
    {
        $redata = (new SupplierBak())->where([['user_id', '=', $data['user_id']]])->first();
        if($redata){
            $redata->host_front_card = !empty($redata->front_card)?Url::getHttpHost().'upload/'. $redata->front_card:null;
            $redata->host_behind_card = !empty($redata->behind_card)?Url::getHttpHost().'upload/'. $redata->behind_card:null;
//            $redata->host_license_img = Url::getHttpHost().'upload/'. $redata->license_img;
            $lit = [];
            if(is_array($redata->license_img)){
                if(!empty($redata->license_img)){
                    foreach ($redata->license_img as $k=>$v){
                        $lit[] = Url::getHttpHost().'upload/'.$v;
                    }
                }
            }
            $redata->host_license_img = $lit;
            return $redata->toArray();
        }
        return "暂无数据";
    }


    /***
     * @param $data
     * 申请供应商
     */
    public function addSuppliers($data)
    {
        try {
            DB::beginTransaction(); // 开始事务
            if (isset($data['license_img'])) {
                $data['license_img'] = Tool::handelString($data['license_img']);
            }
            $redata = (new SupplierBak())->where([['user_id', '=', $data['user_id']]])->first();
            if ($redata) { // 更新
                $redata->account = isset($data['phone']) ? $data['phone'] : $redata->account;
                $redata->pass = isset($data['pass']) ? $data['pass'] : $redata->pass;
                $redata->passwd = isset($data['pass']) ? $data['pass'] : $redata->passwd;
                $redata->user_name = isset($data['user_name']) ? $data['user_name'] : $redata->user_name;
                $redata->phone = isset($data['phone']) ? $data['phone'] : $redata->phone;
                $redata->front_card = isset($data['front_card']) ? $data['front_card'] : $redata->front_card;
                $redata->behind_card = isset($data['behind_card']) ? $data['behind_card'] : $redata->behind_card;
                $redata->company = isset($data['company']) ? $data['company'] : $redata->company;
                $redata->license_img = isset($data['license_img']) ? $data['license_img'] : $redata->license_img;
                $redata->remark = isset($data['remark']) ? $data['remark'] : $redata->remark;
                $res = $redata->save();
            } else {
                //添加
                $this->user_id = isset($data['user_id'])?$data['user_id']:0;
                $this->account = isset($data['phone'])?$data['phone']:"";
                $this->pass = isset($data['pass'])?$data['pass']:"";
                $this->passwd = isset($data['passwd'])?$data['passwd']:"";
                $this->user_name = isset($data['user_name'])?$data['user_name']:"";
                $this->phone = isset($data['phone'])?$data['phone']:"";$data['phone'];
                $this->front_card = isset($data['front_card'])?$data['front_card']:"";
                $this->behind_card = isset($data['behind_card'])?$data['behind_card']:"";
                $this->company = isset($data['company'])?$data['company']:"";
                $this->license_img = isset($data['license_img'])?$data['license_img']:"";
                $this->remark = isset($data['remark']) ? $data['remark'] : "";
                $this->is_work = isset($data['is_work']) ? $data['is_work'] : 0;
                $res = $this->save();
            }
            if ($res) {
                DB::commit(); // 提交事务
                return ['supplier_id' => $this->id ?: $redata->id];
            } else {
                return '入驻失败';
                DB::rollback(); // 回滚事务
            }
        } catch (Exception $e) {
            DB::rollback(); // 回滚事务
        }
    }

    /***
     * @param $data
     * @return mixed
     * 查看账号是否存在
     */
    public function getSupplierAccount($wid)
    {
        return (new Supplier())->where("account", '=', $wid)->first();
    }

    /**
     * @param $id
     * @return mixed
     * 查询供应商信息
     */
    public function getInfo($id)
    {
        return $this->where([['id', '=', $id]])->first();
    }
}
